package com.zyx.javademo.nio.channel;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/17 13:33
 * desc: 基于transferTo方法在两个通道间传输数据
 */
public class FileChannelTransferTo {
    public static void main(String[] args) {
        try ( // 创建读取的channel
              final RandomAccessFile fromFile = new RandomAccessFile("input/nio/read02.txt", "rw");
              final FileChannel fromChannel = fromFile.getChannel();
              // 创建写入的channel
              final RandomAccessFile toFile = new RandomAccessFile("input/nio/write02.txt", "rw");
              final FileChannel toChannel = toFile.getChannel()) {

            // 通过transferTo方法传输数据
            int position = 0;
            fromChannel.transferTo(position, fromChannel.size(), toChannel);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("end...");
        
    }
}
